package com.copyctrl.admin.controller;

import cn.hutool.json.JSONUtil;
import com.copyctrl.admin.dto.PassageDTO;
import com.copyctrl.admin.service.PassageService;
import com.copyctrl.campus.common.model.ApiResponse;
import com.copyctrl.feignapi.client.PassageClient;
import com.copyctrl.model.CampPassage;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author:JH
 * @version:1.0
 */
@NoArgsConstructor
@Data
@RestController
@RequestMapping("/passage")
public class PassageController {
    @Autowired
    PassageService passageService;

    @Autowired
    PassageClient client;

    @PostMapping()
    public ApiResponse addPassage(@RequestBody Map passage) {
        CampPassage campPassage = JSONUtil.toBean((String) passage.get("passage"), CampPassage.class);
        int i = passageService.addPassage(campPassage);
        return i==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("失败");
    }

    @PutMapping
    public ApiResponse updatePassage(@RequestBody Map passage) {
        CampPassage campPassage = JSONUtil.toBean((String) passage.get("passage"), CampPassage.class);
        PassageDTO old = JSONUtil.toBean((String) passage.get("old"), PassageDTO.class);
        System.out.println(old);
        int i = passageService.updatePassage(campPassage,old);
        return i==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("失败");
    }

    @PostMapping("/delete")
    public ApiResponse deletePassage(@RequestBody Map passage) {
        PassageDTO campPassage = JSONUtil.toBean((String) passage.get("passage"), PassageDTO.class);
        System.out.println(campPassage);
        int i = passageService.deletePassage(campPassage);
        return i==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("失败");
    }

    @GetMapping("/all")
    public ApiResponse getAllPassages(){
        List<CampPassage> campPassages = passageService.getallPassages();
        return ApiResponse.ofSuccess(campPassages);
    }

    @GetMapping("/kind/all")
    public ApiResponse getAllKinds(){
        return client.getAllKinds();
    }

    @GetMapping("/detail/{id}")
    public ApiResponse getDetail(@PathVariable Integer id){
        return client.getById(id);
    }

    @GetMapping("/kind/{kind}")
    public ApiResponse getByKind(@PathVariable String kind){
        var byKind = passageService.getByKind(kind);
        return ApiResponse.ofSuccess(byKind);
    }

}
